#include "editdictionary.h"
#include "ui_editdictionary.h"

EditDictionary::EditDictionary(qlonglong id,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditDictionary)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/image/res/stm.jpg"));
    setWindowTitle("编辑字典");
    setFixedSize(458,297);

    this->id = id;
    if(id > 0){
        init();
    }
}

EditDictionary::~EditDictionary()
{
    delete ui;
}

void EditDictionary::on_btn_cancel_clicked()
{
    close();
}

void EditDictionary::init()
{
    QByteArray *data = api.getById(this->id);
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    if(obj.value("code").toInt() != 200) return;
    dict = DictType::toDictType(obj.value("data").toObject());
    ui->lineEdit_dictName->setText(dict.dictName);
    ui->lineEdit_dictType->setText(dict.dictType);
    dict.status == "0" ? ui->radioButton_normal->setChecked(1) :ui->radioButton_stop->setChecked(1);
    ui->lineEdit_remark->setText(dict.remark);
}


void EditDictionary::on_btn_ok_clicked()
{
    dict.dictName = ui->lineEdit_dictName->text();
    dict.dictType = ui->lineEdit_dictType->text();
    dict.status = ui->radioButton_normal->isChecked() ? "0" : "1";
    dict.remark = ui->lineEdit_remark->text();
    QByteArray *data = api.editDict(this->id,dict);
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    if(obj.value("code").toInt() == 200){
        QMessageBox::information(this,"提示",obj.value("msg").toString());
        emit success();
        close();
    }
}

